package cn.itsource.org.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdUtil;
import cn.itsource.basic.config.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.RedisService;
import cn.itsource.basic.util.message.SendMailService;
import cn.itsource.org.pojo.domain.Employee;
import cn.itsource.org.pojo.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop, ShopQuery> implements IShopService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private SendMailService sendMailService;

    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {

        /**
         * 主体
         */
        //1.新增店长的信息到t_employee表，可以拿到店长的id
        Employee admin = shop.getAdmin();

        /**
         * 细节
         * 1.验证两次密码一样 ，密码必须是6位-18位
         * 2.店铺要验证重复 ，手机号，邮箱验证合法性
         */
        if (!StringUtils.equals(admin.getPassword(),admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致");
        }
        //通过hutool验证手机号和邮箱
        boolean isMobile = Validator.isMobile(admin.getPhone());
        boolean isEmail = Validator.isEmail(admin.getEmail());
        if (!isMobile){
            throw new BusinessException("手机号有问题");
        }

        if (!isEmail){
            throw new BusinessException("邮箱有问题");
        }

        //验证店铺重复
        List<Employee> employees =  shopMapper.selectByUsernameOrPhoneOrEmail(admin);
        if (CollectionUtil.isNotEmpty(employees)){
            throw new BusinessException("店铺有可能手机号重复了，或者邮箱重复，或者用户名重复了，请你好好检查一下");
        }

        //1.1搞个盐
       /*
        这两种写法效果是一样，只是没有注释的那种比较悠亚
        String salt = IdUtil.fastSimpleUUID();
        //1.2加盐加密
        String pwd = MD5.create().setSalt(salt.getBytes()).digestHex(admin.getPassword());
        admin.setSalt(salt);
        admin.setPassword(pwd);*/

        //1.1加密加盐
        admin.init();

        employeeMapper.insertSelective(admin);

        //2.新增店铺信息(可以拿到主键)，店长的id（admin_id）通过第一步拿到
        Long adminId = admin.getId();
        shop.setAdminId(adminId);
        shopMapper.insertSelective(shop);

        //3.修改员工，为了设置shop_id外键的值
        admin.setShopId(shop.getId());
        employeeMapper.updateByPrimaryKeySelective(admin);

        //生成uuid
        String uuid = IdUtil.fastSimpleUUID();

        //放到redis里面
        redisService.setStringKeyAndValue(uuid,shop.getId(),1);

        //4.发送邮件
        sendMailService.send("宠物乐园激活","宠物雷院激活链接,http://localhost/shop/active/"+uuid,admin.getEmail());

    }

    @Override
    public AjaxResult active(String uuid) {

        //拿到redis的key  value是要激活的店铺id


        //1.通过uuid（之前存到redis的key），拿到店铺id
        String shopId = redisService.getKeyStringValue(uuid);

        //2.设置state =2
        Shop shop = new Shop();
        shop.setId(Long.valueOf(shopId));
        shop.setState(2);

        //3.修改这条数据，搞定
        shopMapper.updateByPrimaryKeySelective(shop);

        return AjaxResult.createSuccess("激活成功，congsdklfjdls");

    }
}